@extends('layout.master')

@section('title','题库管理')

@section('css')
    @parent
@endsection
@section('main')

    <!-- form itself -->
    <form id="test-form" class="mfp-hide white-popup-block floating-labels items-add"  enctype="multipart/form-data" method="post" action="{{ url('/runGame/items/add') }}">
        <h4><span style="color: red;">*文件名不要有空格和符号</span></h4>
        <hr>
        <div class="form-group m-b-40 m-t-10">
            <input type="file" class="form-control" id="items" name="items">
        </div>
        <div class="form-actions">
            <div class="row">
                <div class="col-md-12">
                    <div class="row">
                        <div class="col-md-offset-3 col-md-9">
                            <button type="submit" class="btn btn-success"><i
                                        class="fa fa-check"></i> 导入
                                {!! csrf_field() !!}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>


    <div class="panel">
        <div class="panel-heading">题库管理</div>
        <div class="panel-action" style="margin-top: -9px">
            <a class="popup-with-form btn fcbtn btn btn-outline btn-info btn-1c" href="#test-form">
                <span>导入题库</span>
            </a>

        </div>
        <div class="panel-wrapper collapse in" aria-expanded="true">
            <div class="panel-body">
                <div class="table-responsive">
                    <table id="example23" class="display nowrap" cellspacing="0" width="100%"
                           data-url="{{ url(request()->getUri()) }}">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>题目 </th>
                            <th>创建时间</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('js')
    @parent
    <script src="{{ asset('js/myself.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup.min.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup-init.js') }}"></script>
    <script>
        $('.items-add').submit(function (e) {
            e.preventDefault();
            var file = document.getElementById("items").files;
            if (!file.length) {
                swal('没有获取到文件');
                return false;
            }
            var formData = new FormData();
            formData.append('items', file[0]);

            swal({
                    title: "",
                    text: "确定导入吗？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({
                        type: "POST",
                        url: '/runGame/items/add',
                        data: formData,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        cache: false,
                        contentType: false,
                        async: false,
                        processData: false,
                        error: function (err) {
                            for (var j in err.responseJSON) {
                                err.responseJSON[j].forEach(function (item, index) {
                                    swal(item);
                                })
                            }
                        },
                        success: function (data) {
                            console.log(data.message);
                            if (data.status === 1) {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.reload();
                                });
                            } else {
                                swal(data.message);
                            }
                        }
                    });
                }
            );
        });
    </script>
@endsection